﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class UserTokenDAO
    {
        /// <summary>
        /// 获取用户Token
        /// </summary>
        /// <returns></returns>
        public IList<UserToken> GetList()
        {
            IList<UserToken> list = new List<UserToken>();
            string sql = "SELECT * FROM UserToken";
            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(sql))
            {
                while (dr.Read())
                {
                    UserToken obj = new UserToken();
                    obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                    obj.UserId = (dr["UserId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("UserId")));
                    obj.OsType = (dr["OsType"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("OsType")));
                    obj.Token = (dr["Token"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("Token")));
                    list.Add(obj);
                }
                dr.Close();
            }
            return list;
        }
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="ent">实体</param>
        /// <returns></returns>
        public int SaveData(UserToken ent)
        {
            string sql = "[Proc_Add_UserToken]";
            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@UserId",SqlDbType.Int),
                new SqlParameter("@OsType",SqlDbType.Int),
                new SqlParameter("@Token",SqlDbType.NVarChar)
            };
            param[0].Value = ent.UserId;
            param[1].Value = ent.OsType;
            param[2].Value = ent.Token;
            return DbHelperSQL.RunProcedure(sql, param);
        }
    }
}
